/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_05_swing.Example_02_Shape.java]
 * Creation Date 	[21-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Java 2D 图形
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_02_Shape {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ShapeFrame frame = new ShapeFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setResizable(false);
			}
		});
	}

}

/**
 * 绘制图形的窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ShapeFrame extends JFrame {

	private static final long serialVersionUID = 4844170854555269988L;

	private static final int DEFAULT_WITHD = 800;
	private static final int DEFAULT_HEIGHT = 800;

	/**
	 * 构造方法
	 */
	public ShapeFrame() {
		this.setTitle("Java 2D shape");
		this.setSize(DEFAULT_WITHD / 2, DEFAULT_HEIGHT / 2);
		this.setLocationByPlatform(true);
		this.getContentPane().add(new JPanel() {
			private static final long serialVersionUID = -9110324694490884159L;

			/**
			 * 不要自己调用paintComponent方法, 如果需要强制重绘, 调用repaint方法
			 */
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2 = (Graphics2D) g;
				double leftX = 100;// 左上角点的x坐标
				double leftY = 100; // 左上角点的y坐标
				double width = 200; // 矩形/椭圆外接矩形的宽度
				double height = 150;// 矩形/椭圆外接矩形的高度

				// 绘制矩形
				Rectangle2D rectangle = new Rectangle2D.Double(leftX, leftY,
						width, height);
				// 使用左上角点和右下角点构造矩形, 首先创建一个空矩形
				// Rectangle2D rectangle = new Rectangle2D.Double();
				// rectangle.setFrameFromDiagonal(x1, y1, x2, y2);
				g2.draw(rectangle);

				// 绘制椭圆
				Ellipse2D ellipse = new Ellipse2D.Double();
				ellipse.setFrame(rectangle); // 使用外接矩形绘制椭圆
				g2.draw(ellipse);

				// 绘制圆形
				double centerX = rectangle.getCenterX();
				double centerY = rectangle.getCenterY();
				double radius = 150;
				Ellipse2D circle = new Ellipse2D.Double();
				circle.setFrameFromCenter(centerX, centerY, centerX + radius,
						centerY + radius); // 使用圆心和另外一点绘制圆形
				g2.draw(circle);

				// 绘制点
				@SuppressWarnings("unused")
				Point2D point = new Point2D.Double(centerX, centerY);

				// 绘制直线
				Line2D line = new Line2D.Double(leftX, leftY, leftX + width,
						leftY + height);
				g2.draw(line);
			}
		});
	}
}
